package com.sgb.capital.view.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import com.google.gson.Gson;
import com.sgb.capital.R;
import com.sgb.capital.base.BaseActivity;
import com.sgb.capital.callback.Constants;
import com.sgb.capital.databinding.ReimbursedetailsActivityBinding;
import com.sgb.capital.model.Bean;
import com.sgb.capital.model.RefundDetailBean;
import com.sgb.capital.model.ReimburseBean;
import com.sgb.capital.utils.Utils;
import com.sgb.capital.view.pop.TipPop;
import com.sgb.capital.view.ui.adapter.BalanceDetailsAdapter;
import com.sgb.capital.viewmodel.CostQueryModel;

import java.util.ArrayList;
import java.util.List;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

/**
 * 作者:张磊
 * 日期:2021/12/16 0016
 * 说明:退款详情
 */
public class ReimburseDetailsActivity extends BaseActivity {
    private ReimbursedetailsActivityBinding mBinding;
    private BalanceDetailsAdapter mAdapter;
    private CostQueryModel mModel;
    private ReimburseBean.ListDTO mData;
    private TipPop mTipPop;
    private List<Bean> mBeans;
    private RefundDetailBean mDetailBean;


    public static void start(Context context, String data) {
        Intent intent = new Intent(context, ReimburseDetailsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.putExtra("data", data);
        context.startActivity(intent);
    }


    @Override
    protected void initView() {
        mModel = new ViewModelProvider(this).get(CostQueryModel.class);
        mTipPop = new TipPop(this);
        mTipPop.setOutSideDismiss(false);
        mBinding = DataBindingUtil.setContentView(this, R.layout.reimbursedetails_activity);
        mBinding.rv.setLayoutManager(new GridLayoutManager(Utils.getContext(), 1));
        mAdapter = new BalanceDetailsAdapter(null);
        mBinding.rv.setAdapter(mAdapter);
    }

    public void initData() {
        mData = new Gson().fromJson(getIntent().getStringExtra("data"), ReimburseBean.ListDTO.class);
    }

    public void initListener() {
        mTipPop.mTvCancel.setOnClickListener(v -> {
            mTipPop.dismiss();
            mModel.getRefundDetail(this, mData.orderId, mData.tradeOrderId);
        });
        mBinding.tvLook.setOnClickListener(v -> {
            CauseDetailActivity.start(this, "", mDetailBean.flowNo);
        });
        mBinding.tvBtn.setOnClickListener(v -> {
            mModel.getRefundStatus(this, mData.orderId);
        });
        mBinding.ivBack.setOnClickListener(v -> finish());
    }

    @Override
    public void initObserve() {
        super.initObserve();
        mModel.mCode.observe(this, s -> {
            if (mBinding.tvBtn.getText().toString().contains("处理")) {
                if (mData.status != s) {
                    mTipPop.initStr("退款状态已发生变化，请刷新后再进行操作。", null, "知道了");
                    mTipPop.show();
                } else {
                    mTipPop.dismiss();
                    RefundActivity.start(this, new Gson().toJson(mDetailBean));
                }
            } else {
                if (mData.status != s) {
                    mTipPop.initStr("已完成退款支付，请刷新页面后再进行操作。", null, "知道了");
                    mTipPop.show();
                } else {
                    mTipPop.initStr("请前往PC端完成退款支付！", null, "知道了");
                    mTipPop.show();
                }
            }
        });


        mModel.mRefundDetailBean.observe(this, data -> {
            mDetailBean = data;
            mBinding.tvBtn.setVisibility((data.status == 0 || data.status == 4 || data.status == 1) ? View.VISIBLE : View.GONE);
            mBinding.tvBtn.setText(data.status == 0 ? "立即处理" : "继续退款");
            mBinding.tvLook.setVisibility(data.status != 6 && !Utils.isEmpty(mDetailBean.flowNo) ? View.VISIBLE : View.GONE);
            if (mBinding.tvLook.getVisibility() == View.VISIBLE) {
                mBinding.tvBtn.setVisibility(View.GONE);
            }
            mBinding.tvReason.setVisibility((data.status == 3 || data.status == 6) ? View.VISIBLE : View.GONE);
            mBinding.tvReason.setText(data.status == 3 ? ("失败原因：" + data.retMsg) : ("拒绝原因：" + data.applyRemark));
            mBinding.ivIcon.setImageResource(
                    data.status == 0 ? R.mipmap.daichuli :
                            (data.status == 1 || data.status == 4 || data.status == 5) ? R.mipmap.withdraw_load :
                                    data.status == 2 ? R.mipmap.succeed_icon :
                                            R.mipmap.icon_withdraw_fail);
            mBinding.tvTip.setText(
                    data.status == 0 ? "待处理" :
                            (data.status == 1 || data.status == 4 || data.status == 5) ? "退款中" :
                                    data.status == 2 ? "已完成" :
                                            data.status == 3 ? "已失败" :
                                                    "已拒绝");
            mBinding.tvTime.setText(data.handleTime == 0 ? "/" : Utils.getTime(data.handleTime));
            if (data.handleTime == 0) {
                mBinding.tvTime.setVisibility(View.GONE);
            }
            if (mBeans != null) mBeans.clear();
            mBeans = new ArrayList<>();
            mBeans.add(new Bean("业务类型", data.oriTradeType));
            mBeans.add(new Bean("退费单号", data.orderId));
            mBeans.add(new Bean("原订单号", data.oriOrderId));
            mBeans.add(new Bean("原交易流水号", data.oriMerTrace));
            mBeans.add(new Bean("原交易金额", "￥" + Utils.getDecimalFormat(data.oriAmount / 100.00)));
            mBeans.add(new Bean("退款金额", "￥" + Utils.getDecimalFormat(data.amount / 100.00)));
            mBeans.add(new Bean("申请商户", data.applyName));
            mBeans.add(new Bean("申请时间", Utils.getTime(data.applyTime)));
            mBeans.add(new Bean("操作人", data.handleName));
            mBeans.add(new Bean("处理时间", data.handleTime == 0 ? "/" : Utils.getTime(data.handleTime)));
            mAdapter.setNewInstance(mBeans);
            mBinding.viewNull.setVisibility(View.GONE);
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        mModel.getRefundDetail(this, mData.orderId, mData.tradeOrderId);
    }

    @Override
    protected void onDestroy() {
        Utils.sendMsg(Constants.EVENT_REFRESH, null);
        super.onDestroy();
    }
}